package com.aaron.demo1.test.megaeyes.service.impl;

import cn.hutool.http.HttpRequest;
import com.aaron.demo1.test.megaeyes.model.PTZControl;
import com.aaron.demo1.test.megaeyes.service.PTZControlService;
import com.alibaba.fastjson.JSONObject;
import ideal4j.icity.common.enumtype.CodeEnumType;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>云台转动service</p>
 *
 * @author Chenzy
 * @date 2021/3/15 14:26
 **/
@Service
public class PTZControlServiceImpl implements PTZControlService {

    @Autowired
    private ApiServiceImpl apiService;

//    @Autowired
//    private MegaEyesMapper megaEyesMapper;

    private static final Logger logger = LoggerFactory.getLogger(PTZControlServiceImpl.class);

    public static final String ACTION = "/videoService/ptz/";

    public static final String KEEP_ALIVE = "/videoService/accounts/token/keepalive";

    public static final double DEFAULT_SPEED = 0.5;

    public static final int DEFAULT_DURATION_TIME = 500;

    public static final int TIME_OUT_IN_MILLISECONDS = 5 * 1000;


    //此处为预置点编号
    public static final String INDEX = "11";

    public static final String GET_PRESET = "/videoService/ptz/preset/goto/channels/";

//    @Autowired
//    private VideoEquipmentInfoMapper videoEquipmentInfoMapper;
//
//    @Autowired
//    private RedisTemplate redisTemplate;

    private String token;

    public int move(String direction, String equipmentId,int pos) {

        PTZControl ptz = new PTZControl();
        String content = "";
        switch (direction) {
            case "up":
                ptz.setTilt(DEFAULT_SPEED);
                break;
            case "down":
                ptz.setTilt(-DEFAULT_SPEED);
                break;
            case "left":
                ptz.setPan(-DEFAULT_SPEED);
                break;
            case "right":
                ptz.setPan(DEFAULT_SPEED);
                break;
            case "leftUp":
                ptz.setPan(-DEFAULT_SPEED);
                ptz.setTilt(DEFAULT_SPEED);
                break;
            case "leftDown":
                ptz.setPan(-DEFAULT_SPEED);
                ptz.setTilt(-DEFAULT_SPEED);
                break;
            case "rightUp":
                ptz.setPan(DEFAULT_SPEED);
                ptz.setTilt(DEFAULT_SPEED);
                break;
            case "rightDown":
                ptz.setPan(DEFAULT_SPEED);
                ptz.setTilt(-DEFAULT_SPEED);
                break;
            case "zoomIn":
                ptz.setZoom(DEFAULT_SPEED);
                break;
            case "zoomOut":
                ptz.setZoom(-DEFAULT_SPEED);
                break;
            case "stop":
//                ptz.setPan(0.0);
//                ptz.setTilt(0.0);
//                ptz.setZoom(0.0);
                break;
            default:
                return CodeEnumType.INVALID_PARAM.code;
        }

        ptz.setDuration(DEFAULT_DURATION_TIME);
        String lesseeId = "";
//        if (StringUtils.isEmpty(token))
            token = apiService.getToken(lesseeId);




//        String channelCode1 = "xz46DizuB1D01PFSKPTOTK";
//        String channelCode2 = "xz46DizuB1D01PFSKQS9O0";
//
////        String channelCode = getChannelCode(equipmentId);
//        String channelCode = channelCode1;
        String channelCode = "";

        if (pos == 0) {
            channelCode = "xz46DizuB1D01PFU511NBB";
        }else{
            channelCode = "xz46DizuB1D01PFU52UOQ4";
        }

        String url = "https://61.151.156.1:8282"+ ACTION + channelCode;
        content = JSONObject.toJSONString(ptz);

        String response = HttpRequest.put(url).header("X-Subject-Token",token).body(content).timeout(TIME_OUT_IN_MILLISECONDS).execute().body();

        if (StringUtils.isNotEmpty(response)) {
            token = apiService.getToken(lesseeId);
            response = HttpRequest.put(url).body(content).timeout(TIME_OUT_IN_MILLISECONDS).execute().body();
        }
//        redisTemplate.opsForValue().set("dahua:" + channelCode1, "", 5, TimeUnit.MINUTES);
        return CodeEnumType.SUCCESS.code;

    }

//    public int getPreset(String equipmentId) {
//        /**
//         * speed:类型double ，云台运行速度，归一化 0~1 ，如果没有speed字段表示默认速度
//         */
//        String content = "{\"speed\" : 0.5}";
//        String channelCode = this.getChannelCode(equipmentId);
//        String url = GET_PRESET + channelCode + "/" + INDEX;
//        String response = HttpTestUtils.httpRequest(HttpEnum.PUT, property.getUrl(), url, token, content);
//        return CodeEnumType.SUCCESS.code;
//    }

//
//    @Scheduled(fixedRate = 110000)
//    public void keepAlive() {
//        if (StringUtils.isEmpty(token)) {
//            logger.info("无token，开始获取token");
//            token = apiService.getToken();
//        }
//        logger.info("定时任务，开始执行保活,当前token为:" + token);
//        String content = "{\"token\":\"" + token + "\"}";
//        String response = HttpTestUtils.httpRequest(HttpEnum.PUT, property.getUrl(), KEEP_ALIVE, token, content);
//        JSONObject jsonObject = JSON.parseObject(response);
//        if ((int) jsonObject.get("code") == 200)
//            logger.info("保活成功!");
//    }
//
//    @TargetDataSource("icts")
//    private String getChannelCode(String equipmentId) {
//        String storageChannel = megaEyesMapper.getStorageChannelByEquipmentId(equipmentId);
//        return storageChannel;
//    }

//
//    public String getEquipmentId(String storageChannel) {
//        Example example = new Example(VideoEquipmentInfo.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storageChannel", storageChannel).andIsNotNull("equipmentId");
//        List<VideoEquipmentInfo> videoEquipmentInfos = videoEquipmentInfoMapper.selectByExample(example);
//        String equipmentId = "";
//        if (videoEquipmentInfos.size() > 0) {
//            equipmentId = videoEquipmentInfos.get(0).getEquipmentId();
//        }
//        return equipmentId;
//    }
}